package com.swsc.solarverse.service.analysis.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swsc.solarverse.dao.entity.analysis.CorporationEfficiencyRecordDO;
import com.swsc.solarverse.dao.mapper.analysis.CorporationEfficiencyRecordMapper;
import com.swsc.solarverse.service.analysis.ICorporationEfficiencyRecordService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2024-07-02
 */
@Service
public class CorporationEfficiencyRecordServiceImpl extends ServiceImpl<CorporationEfficiencyRecordMapper, CorporationEfficiencyRecordDO> implements ICorporationEfficiencyRecordService {

    @Override
    public int insertIgnoreBatch(List<CorporationEfficiencyRecordDO> list) {
        return baseMapper.insertIgnoreBatch(list);
    }

    @Override
    public Double avgLossE(Collection<Integer> organNoIn, LocalDateTime dataTimeGe, LocalDateTime dataTimeLt) {
        return baseMapper.avgLossE(organNoIn, dataTimeGe, dataTimeLt);
    }

    @Override
    public LocalDateTime maxDataTime(Integer organNo) {
        return baseMapper.maxDataTime(organNo);
    }

    @Override
    public CorporationEfficiencyRecordDO getRecord(Integer tenantNo, Integer organNo, LocalDateTime dataTime) {
        return baseMapper.selectOne(Wrappers.lambdaQuery(CorporationEfficiencyRecordDO.class)
                .eq(null != tenantNo, CorporationEfficiencyRecordDO::getTenantNo, tenantNo)
                .eq(null != organNo, CorporationEfficiencyRecordDO::getOrganNo, organNo)
                .eq(null != dataTime, CorporationEfficiencyRecordDO::getDataTime, dataTime)
                .orderByDesc(CorporationEfficiencyRecordDO::getDataTime, CorporationEfficiencyRecordDO::getId)
                .last("LIMIT 1")
        );
    }

    @Override
    public CorporationEfficiencyRecordDO getLatestRecord(Integer tenantNo, Integer organNo) {
        return getRecord(tenantNo, organNo, null);
    }
}
